#coding=utf-8
import requests
import re
import threading
import time
import math
class myThead(threading.Thread):
    def __init__(self,url,k,jap,ft):
        threading.Thread.__init__(self)
        self.url=url
        self.jap=jap
        self.k=k
        self.ft=ft
    def run(self):
        for j in range(self.k, self.k+self.jap):
            r=requests.get(self.url+str(j))
            if r.status_code==404:
                print "over"
                break
            strs = re.findall(r'<h3 class="course-card-name">(.*?)</h3>', r.content)
            for i in strs:
                print i
            del strs
            del r
        print "time:",time.time()-self.ft

def mooc():
    url="https://www.imooc.com/course/list?page="
    pagenum=35
    thnum = 13
    ft = time.time()
    thrs = []
    jap=int(math.ceil(pagenum/float(thnum)))
    for k in range(1,pagenum,jap):
        thrs.append(myThead(url,k,jap,ft))
    for sk in thrs:
        sk.start()
if __name__=="__main__":
    mooc()